<template>
<div class="content-box">
    <heading grade="1">{{title}}</heading>
    <div class="add_class">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="课程名称：" prop="className">
          <el-select v-model="form.className" value-key="id" placeholder="请选择课程" style="width: 550px;" :disabled="dedaultData.isExistStudent ? dedaultData.isExistStudent : false" @change="handleGetschollType">
              <el-option v-for="item in train_selectclass_list" :key="item.id" :label="item.courseName" :value="item"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="校 区：" prop="campus">
          <el-select v-model="form.campus" value-key="key" placeholder="请选择校区" no-data-text="请先选择课程或到校区管理设置" style="width: 550px;" :disabled="dedaultData.isExistStudent ? dedaultData.isExistStudent : false" @change="hanleGetAddress">
            <el-option v-for="item in train_scholl_type_list" :key="item.key" :label="item.value" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班 别：" prop="type">
          <el-select v-model="form.type" value-key="key" placeholder="请选择班别" style="width:550px;" :disabled="dedaultData.isExistStudent ? dedaultData.isExistStudent : false">
            <el-option v-for="item in train_class_type_list" :key="item.key" :label="item.value" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班级名称：" prop="name">
          <el-input v-model="form.name" style="width:550px;"></el-input>
        </el-form-item>
        <el-form-item label="上课时间：" prop="time">
          <el-date-picker
            v-model="form.time"
            type="daterange"
            :editable="false"
            align="right"
            format="yyyy-MM-dd"
            :clearable="false"
            @change="handleSelectTime"
            placeholder="选择日期范围"
            style="width:550px;">
          </el-date-picker>
        </el-form-item>
        <!--专业课-->
        <div class="specialty" v-if="form.className.properties == 0">
          <el-form-item label="课程教练：" prop="coach">
            <el-select v-model="form.coach" value-key="id" placeholder="请选择教练" style="width: 550px;">
              <el-option v-for="item in train_teacher_list" :key="item.id" :label="item.teacherName" :value="item"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="上课周期：" required>
            <el-row v-for="(item, index) in form.classSeting" :key="index">
              <el-col :span="2" style="margin-bottom:0;">每{{Number(item.weekTime) | getWeekDay}}</el-col>
              <el-col :span="22" style="margin-bottom:0;" v-for="(classItem, classIndex) in item.setArray" :key="index+classIndex" :offset="classIndex > 0 ? 2 : 0">
                <el-row style="margin-bottom:20px;">
                  <el-col :span="7">
                    <el-form-item :class="['relative',{'is-error': classItem.isError == 1 || classItem.isTimeok == 1}]">
                      <el-time-picker
                        is-range
                        v-model="classItem.classTime"
                        format="HH:mm"
                        :clearable="false"
                        placeholder="选择上课时间"
                        @change="handleTimeSection(classItem.classTime, index, classIndex)"
                        style="width:185px;">
                      </el-time-picker>
                      <div class="el-form-item__error" v-show="classItem.isError == 1 && !classItem.isTimeok != 1">上课时间重复</div>
                      <div class="el-form-item__error" v-show="classItem.isError != 1 && classItem.isTimeok == 1">上下课时间须相差10分钟</div>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-select v-model="classItem.classAdd" value-key="id" placeholder="选择上课地点" style="width: 95%;" no-data-text="请先选择校区或者到校区管理处设置" @change="handleCheckOneTeacher(index, classIndex)">
                      <el-option v-for="item in addressList" :key="item.id" :label="item.address" :value="item"></el-option>
                    </el-select>
                  </el-col>
                  <el-col :span="2">
                    <el-button class="btn-icon" icon="plus" @click="handleAddClassItem(index, classIndex)" v-if="((classIndex == item.setArray.length - 1) || item.setArray.length == 1) && item.setArray.length < 4" :disabled="!(classItem.classTime && classItem.classAdd && classItem.isError == 2)"></el-button>
                    <el-button class="btn-icon" icon="minus" @click="handleRemoveClassItem(index, classIndex)" v-if="(classIndex != item.setArray.length - 1) && item.setArray.length != 1"></el-button>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <el-row style="margin-bottom:0;">
              <el-col :span="2" class="set_all">批量设置</el-col>
              <el-col :span="22" style="margin-bottom:0;">
                <el-row style="margin-bottom:0px;">
                  <el-col :span="7">
                    <el-time-picker
                      is-range
                      :clearable="false"
                      format="HH:mm"
                      v-model="form.batchSet.classTime"
                      placeholder="选择上课时间" style="width:185px;">
                    </el-time-picker>
                  </el-col>
                  <el-col :span="8">
                    <el-select v-model="form.batchSet.classAdd" value-key="id" placeholder="选择上课地点" no-data-text="请先选择校区或者到校区管理处设置" style="width: 95%;">
                      <el-option v-for="item in addressList" :key="item.id" :label="item.address" :value="item"></el-option>
                    </el-select>
                  </el-col>
                  <el-col :span="5">
                    <el-button @click="handleBatchSet">确定</el-button>
                    <el-button @click="handleReset">重置</el-button>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </el-form-item>
        </div>
        <!--综合课--> 
        <div v-else>
          <el-form-item label="上课周期：" required :disabled="true">
            <el-row v-for="(item, index) in form.classSeting" :key="index">
              <el-col :span="2" style="margin-bottom:0;">每{{Number(item.weekTime) | getWeekDay}}</el-col>
              <el-col :span="22" style="margin-bottom:0;" v-for="(classItem, classIndex) in item.setArray" :key="index+classIndex" :offset="classIndex > 0 ? 2 : 0">
                <el-row style="margin-bottom:20px;">
                  <el-col :span="5">
                    <el-form-item>
                      <el-input v-model="classItem.className" placeholder="请输入学科名称" style="width:95%;"></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="5">
                    <el-form-item :class="['relative',{'is-error': classItem.isError == 1 || classItem.isTimeok == 1}]">
                      <el-time-picker
                        is-range
                        v-model="classItem.classTime"
                        format="HH:mm"
                        :clearable="false"
                        placeholder="选择上课时间"
                        @change="handleTimeSection(classItem.classTime, index, classIndex)"
                        style="width:135px;">
                      </el-time-picker>
                      <div class="el-form-item__error" v-show="classItem.isError == 1 && classItem.isTimeok != 1">上课时间重复</div>
                      <div class="el-form-item__error" v-show="classItem.isError != 1 && classItem.isTimeok == 1">上下课时间须相差10分钟</div>
                    </el-form-item>
                  </el-col>
                  <el-col :span="5">
                    <el-select v-model="classItem.classAdd" value-key="id" placeholder="选择上课地点" style="width: 95%;" no-data-text="请先选择校区或者到校区管理处设置">
                      <el-option v-for="item in addressList" :key="item.id" :label="item.address" :value="item"></el-option>
                    </el-select>
                  </el-col>
                  <el-col :span="5">
                    <el-select v-model="classItem.classCoach" value-key="id" placeholder="请选择教练" style="width: 100%;" @change="handleCheckTeacher(index, classIndex)">
                      <el-option v-for="item in train_teacher_list" :key="item.id" :label="item.teacherName" :value="item"></el-option>
                    </el-select>
                  </el-col>
                  <el-col :span="2">
                    <el-button class="btn-icon" icon="plus" @click="handleAddClassItem(index, classIndex)" v-if="((classIndex == item.setArray.length - 1) || item.setArray.length == 1) && item.setArray.length < 4" :disabled="!(classItem.className && classItem.classTime && classItem.classAdd && classItem.classCoach && classItem.isError == 2)" :key="index+classIndex"></el-button>
                    <el-button class="btn-icon" icon="minus" @click="handleRemoveClassItem(index, classIndex)" v-if="(classIndex != item.setArray.length - 1) && item.setArray.length != 1"></el-button>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="2" class="set_all">批量设置</el-col>
              <el-col :span="22" style="margin-bottom:0;">
                <el-row>
                 <el-col :span="5">
                    <el-input v-model="form.batchSet.className" placeholder="请输入学科名称" style="width:95%;"></el-input>
                  </el-col>
                  <el-col :span="5">
                    <el-time-picker
                      is-range
                      :clearable="false"
                      v-model="form.batchSet.classTime"
                      format="HH:mm"
                      placeholder="选择上课时间" style="width:135px;">
                    </el-time-picker>
                  </el-col>
                  <el-col :span="5">
                    <el-select v-model="form.batchSet.classAdd" value-key="id" placeholder="选择上课地点" style="width: 95%;" no-data-text="请先选择校区或者到校区管理处设置">
                      <el-option v-for="item in addressList" :key="item.id" :label="item.address" :value="item"></el-option>
                    </el-select>
                  </el-col>
                  <el-col :span="5">
                    <el-select v-model="form.batchSet.classCoach" value-key="id" placeholder="请选择教练" style="width: 100%;">
                      <el-option v-for="item in train_teacher_list" :key="item.id" :label="item.teacherName" :value="item"></el-option>
                    </el-select>
                  </el-col>
                  <el-col :span="4">
                    <el-button style="padding:10px 6px;margin-left:10px;" @click="handleBatchSet">确定</el-button>
                    <el-button style="padding:10px 6px;margin-left:5px;" @click="handleReset">重置</el-button>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </el-form-item>
        </div>
        <!--课时列表-->
        <div class="time_list">
          <el-row>
            <el-col :span="8" v-for="(item, index) in hourList" :key="index" :class="{'color_grea': item.status == 1}">{{item.dateTime + ' ' + item.weekDay + ' ' + item.monent}} <el-button type="text" v-if="item.status == 0" @click="handleDelteHour(index)">删除</el-button><el-button type="text" v-else @click="handleCancleHour(index)">恢复</el-button></el-col>
          </el-row>
          <div class="class_total">共<span class="total_green">{{hourList.length}}</span>课时<p style="display:inline-block;" v-if="deleteHour > 0">，已删除<span class="total_red">{{deleteHour}}</span>课时，还有<span class="total_green">{{hourList.length - deleteHour}}</span>课时</p></div>
        </div>
      </el-form>
      <div class="button-box">
        <el-button type="primary" @click="handleSubmit" :disabled="hourList.length - deleteHour == 0">确 定</el-button>
        <el-button @click="goBackList">返 回</el-button>
      </div>
    </div>
</div>
</template>
<script>
import trainApi from 'api/train'
import { mapGetters } from 'vuex'
import { parseTime } from 'utils'
export default {
  computed: {
    ...mapGetters({
      train_teacher_list: 'train_teacher_list',
      train_selectclass_list: 'train_selectclass_list',
      train_scholl_type_list: 'train_scholl_type_list',
      train_class_type_list: 'train_class_type_list'
    })
  },
  created () {
    this.$store.dispatch('train_teacher_list')
    this.$store.dispatch('train_selectclass_list')
    if (this.$route.query.isedit === 'edit') {
      this.isEditone = true
      trainApi.classEdit({
        id: this.$route.query.id
      }).then((res) => {
        this.dedaultData = res.data
        this.setDefaultData()
      })
    }
  },
  data () {
    return {
      title: '班级信息',
      dedaultData: {},
      schoolList: [],
      gradeList: [],
      isEditone: false,
      isClassSet: true,
      form: {
        className: '',
        campus: '',
        type: '',
        name: '',
        time: [],
        coach: '',
        classSeting: [],
        batchSet: {
          className: '',
          classTime: '',
          classAdd: '',
          classCoach: ''
        }
      },
      hourList: [],
      dataList: [],
      subList: [],
      addressList: [],
      deleteHour: 0,
      rules: {
        className: [
          { type: 'object', required: true, message: '请选择课程', trigger: 'change' }
        ],
        campus: [
          { type: 'object', required: true, message: '请选择校区', trigger: 'change' }
        ],
        type: [
          { type: 'object', required: true, message: '请选择班别', trigger: 'change' }
        ],
        name: [
          { required: true, message: '请输入班级名称', trigger: 'blur' }
        ],
        classType: [
          { required: true, message: '请选择课程类型', trigger: 'change' }
        ],
        time: [
          { required: true, message: '请选择上课时间' },
          {
            validator: (rule, value, callback) => {
              if (this.form.className.startClassTime && this.form.className.endClassTime && ((parseTime(this.form.className.startClassTime, '{y}.{m}.{d}') > parseTime(value[0].getTime(), '{y}.{m}.{d}')) || (parseTime(this.form.className.endClassTime, '{y}.{m}.{d}') < parseTime(value[1].getTime(), '{y}.{m}.{d}')))) {
                callback(new Error('上课日期在' + parseTime(this.form.className.startClassTime, '{y}.{m}.{d}') + ' - ' + parseTime(this.form.className.endClassTime, '{y}.{m}.{d}') + '范围内!'))
              } else {
                callback()
              }
            }
          }
        ],
        coach: [
          { type: 'object', required: true, message: '请选择课程教练', trigger: 'change' }
        ]
      }
    }
  },
  watch: {
    'form.classSeting': {
      handler () {
        if (!this.isEditone) {
          this.gethourData()
        }
        this.getsubData()
      },
      deep: true
    },
    'form.type': function (val, oldVal) {
      if (oldVal) { // 切换班别，清空上课地点
        for (let item of this.form.classSeting) {
          for (let sitem of item.setArray) {
            sitem.classAdd = ''
          }
        }
      }
    }
  },
  methods: {
    goBackList () {
      this.$router.push({path: '/training/class'})
    },
    handleCheckOneTeacher (index, classIndex) { // 专业课教练校验
      if (this.form.classSeting[index].setArray[classIndex].classAdd === '') {
        return false
      }
      if (!(this.form.campus && this.form.type && this.form.time[0] && this.form.coach && this.form.classSeting[index].setArray[classIndex].classTime[0])) {
        this.$message({
          type: 'warning',
          message: '请先选择校区/班别/上课日期/教练/上课时间'
        })
        this.form.classSeting[index].setArray[classIndex].classAdd = ''
      } else {
        this.checkTeacher(this.form.coach.id, index, classIndex)
      }
    },
    handleCheckTeacher (index, classIndex) { // 综合课教练校验
      if (this.form.classSeting[index].setArray[classIndex].classCoach === '') {
        return false
      }
      if (!(this.form.campus && this.form.type && this.form.time[0] && this.form.classSeting[index].setArray[classIndex].classTime[0])) {
        this.$message({
          type: 'warning',
          message: '请先选择校区/班别/上课日期/上课时间'
        })
        this.form.classSeting[index].setArray[classIndex].classCoach = ''
      } else {
        this.checkTeacher(this.form.classSeting[index].setArray[classIndex].classCoach.id, index, classIndex)
      }
    },
    checkTeacher (teacherid, index, classIndex) { // 教练校验请求
      trainApi.checkTeacher({
        courseId: this.form.className.id,
        schoolId: this.form.campus.key,
        weekCode: this.form.classSeting[index].weekCode,
        teacherId: teacherid,
        beginTimeStr: parseTime(this.form.time[0], '{y}-{m}-{d}'),
        endTimeStr: parseTime(this.form.time[1], '{y}-{m}-{d}'),
        classBeginTimeStr: parseTime(this.form.classSeting[index].setArray[classIndex].classTime[0], '{h}:{i}'),
        classEndTimeStr: parseTime(this.form.classSeting[index].setArray[classIndex].classTime[1], '{h}:{i}')
      }).then((res) => {
        if (res.data) {
          this.$message({
            type: 'warning',
            message: '该教练在此时间段已有课程，请重新设置'
          })
        }
      })
    },
    setDefaultData () { // 编辑时设置初始数据
      this.form.className = {id: this.dedaultData.courseId, courseName: ''}
      this.form.name = this.dedaultData.name
      this.form.time = [new Date(Number(this.dedaultData.beginTime)), new Date(Number(this.dedaultData.endTime))]
    },
    handleGetschollType () { // 根据课程ID获取校区班别数据
      this.form.campus = ''
      this.form.type = ''
      if (this.form.className) {
        this.$store.dispatch('train_scholl_type_list', {courseId: this.form.className.id})
        this.$store.dispatch('train_class_type_list', {courseId: this.form.className.id})
        this.setStratClassList()
        if (this.$route.query.isedit === 'edit') {
          for (let i of this.train_selectclass_list) {
            if (i.id === this.dedaultData.courseId) {
              this.form.className.properties = i.properties
            }
          }
          if (this.form.className.properties === 0) {
            this.form.coach = {id: this.dedaultData.listCourseCycle[0].teacherId, value: ''}
          }
          this.form.campus = {key: this.dedaultData.schoolId, value: ''}
          this.form.type = {key: this.dedaultData.classesId, value: ''}
        }
      }
    },
    setStratClassList () { // 设置默认上课周期数据
      this.form.classSeting = []
      if (this.form.className) {
        for (let item of this.train_selectclass_list) {
          if (item.id === this.form.className.id) {
            let codeArr = item.classCycleCode ? item.classCycleCode.split(',').sort() : []
            if (codeArr.length > 0) {
              for (let sitem of codeArr) {
                this.form.classSeting.push({
                  weekTime: sitem,
                  weekCode: sitem,
                  setArray: [
                    {
                      className: '',
                      classTime: '',
                      classAdd: '',
                      classCoach: '',
                      isError: 2,
                      isTimeok: 2
                    }
                  ]
                })
              }
            }
          }
        }
        if (this.$route.query.isedit === 'edit') { // 编辑设置初始值
          let classTimelist = this.dedaultData.listCourseCycle
          for (let i = 0; i < classTimelist.length; i++) { // 编辑时设置初始上课周期数据
            for (let hitem of this.form.classSeting) {
              if (classTimelist[i] && classTimelist[i].weekCode === Number(hitem.weekCode)) {
                let monDay = parseTime(classTimelist[i].createTime, '{y}-{m}-{d}')
                hitem.setArray = []
                hitem.setArray.push({
                  className: classTimelist[i].subjectName,
                  classTime: [new Date(monDay + ' ' + parseTime(classTimelist[i].beginTime, '{h}:{i}')), new Date(monDay + ' ' + parseTime(classTimelist[i].endTime, '{h}:{i}'))],
                  classAdd: {id: classTimelist[i].addressId, address: ''},
                  classCoach: {id: classTimelist[i].teacherId, teacherName: ''},
                  isError: 2,
                  isTimeok: 2
                })
              }
            }
          }
        }
      }
    },
    hanleGetAddress () { // 根据校区获取上课地址
      if (this.form.campus) {
        trainApi.getAddress({
          schoolId: this.form.campus.key
        }).then((res) => {
          this.addressList = res.data
        })
      }
    },
    handleTimeSection (val, index, classIndex) { // 时间区间限制08:00-22:00&&一天之内课时不能重复
      if (val && (parseTime(val[0], '{h}:{i}') < '08:00' || parseTime(val[1], '{h}:{i}') > '22:00')) {
        this.$message({
          type: 'warning',
          message: '上下课时间范围为08:00-22:00!'
        })
        this.form.classSeting[index].setArray[classIndex].classTime = []
      } else if (val && val[0]) {
        if (val[1].getTime() - val[0].getTime() < 600000) {
          this.$set(this.form.classSeting[index].setArray[classIndex], 'isTimeok', 1)
        } else {
          this.$set(this.form.classSeting[index].setArray[classIndex], 'isTimeok', 2)
        }
        if (this.form.classSeting[index].setArray.length > 1) {
          let isError = []
          for (let i = 0; i < this.form.classSeting[index].setArray.length; i++) {
            let item = this.form.classSeting[index].setArray[i]
            if (classIndex !== i) {
              if (!(val[0].getTime() > item.classTime[1].getTime() || val[1].getTime() < item.classTime[0].getTime())) {
                isError.push(false)
              }
            }
          }
          if (isError.includes(false)) {
            this.$set(this.form.classSeting[index].setArray[classIndex], 'isError', 1)
          } else {
            this.$set(this.form.classSeting[index].setArray[classIndex], 'isError', 2)
          }
        }
      }
    },
    getsubData () { // 转换提交数据
      let classSeting = this.form.classSeting
      this.subList = []
      for (let item of classSeting) {
        let setArray = item.setArray
        for (let smItem of setArray) {
          if (smItem.className || smItem.classAdd) {
            this.subList.push({
              subjectName: smItem.className,
              weekCode: item.weekCode,
              beginTime: new Date(smItem.classTime[0]).getTime(),
              endTime: new Date(smItem.classTime[1]).getTime(),
              addressId: smItem.classAdd ? smItem.classAdd.id : '',
              teacherId: smItem.classCoach ? smItem.classCoach.id : ''
            })
          }
        }
      }
    },
    handleSubmit () { // 提交信息
      this.$refs['form'].validate((valid) => {
        if (!this.isClassSet) {
          this.$message({
            type: 'warning',
            message: '上课周期不能为空！'
          })
        } else {
          if (valid) {
            let params = {
              courseId: this.form.className.id,
              schoolId: this.form.campus.key,
              classesId: this.form.type.key,
              name: this.form.name,
              beginTime: this.form.time[0].getTime(),
              endTime: this.form.time[1].getTime(),
              listCourseCycle: this.subList,
              listClassSchedule: this.hourList,
              teacherId: this.form.coach ? this.form.coach.id : '',
              removeClassScheduleCount: this.deleteHour
            }
            let methodType = this.$route.query.isedit === 'edit' ? 'put' : 'post'
            if (this.$route.query.isedit === 'edit') {
              params.id = this.$route.query.id
            }
            trainApi.addClassRoom(JSON.stringify(params), methodType).then((res) => {
              this.$router.push({path: '/training/class'})
            })
          } else {
            return false
          }
        }
      })
    },
    handleAddClassItem (index, classIndex) { // 增加上课时间
      this.$set(this.form.classSeting[index].setArray, classIndex + 1, {
        className: '',
        classTime: '',
        classAdd: '',
        classCoach: '',
        isError: 2
      })
    },
    handleRemoveClassItem (index, classIndex) { // 减少上课时间
      this.form.classSeting[index].setArray.splice(classIndex, 1)
      let isError = []
      for (let i = 0; i < this.form.classSeting[index].setArray.length; i++) {
        let item = this.form.classSeting[index].setArray[i]
        if (classIndex !== i) {
          if (!(this.form.classSeting[index].setArray[classIndex].classTime[0].getTime() > item.classTime[1].getTime() || this.form.classSeting[index].setArray[classIndex].classTime[1].getTime() < item.classTime[0].getTime())) {
            isError.push(false)
          }
        }
      }
      if (isError.includes(false)) {
        this.$set(this.form.classSeting[index].setArray[classIndex], 'isError', 1)
      } else {
        this.$set(this.form.classSeting[index].setArray[classIndex], 'isError', 2)
      }
    },
    handleSelectTime () { // 上课日期选择
      if (!this.form.className) {
        this.$message({
          type: 'warning',
          message: '请选择课程！'
        })
        this.form.time = []
        return false
      }
      let sDate, eDate, dateSpan, days
      sDate = Date.parse(this.form.time[0])
      eDate = Date.parse(this.form.time[1])
      dateSpan = Math.abs(sDate - eDate)
      days = Math.floor(dateSpan / (24 * 3600 * 1000))
      if (days) {
        this.setHourData(sDate, eDate)
        this.gethourData()
      }
    },
    handleBatchSet () { // 批量设置上课时间
      let classSeting = this.form.classSeting
      let batchSeting = this.form.batchSet
      for (let item of classSeting) {
        let setArray = item.setArray
        for (let smItem of setArray) {
          smItem.className = batchSeting.className
          smItem.classTime = batchSeting.classTime
          smItem.classAdd = batchSeting.classAdd
          smItem.classCoach = batchSeting.classCoach
        }
      }
    },
    handleReset () { // 批量重置时间
      this.form.batchSet = {
        className: '',
        classTime: '',
        classAdd: '',
        classCoach: ''
      }
      this.handleBatchSet()
      this.deleteHour = 0
    },
    setHourData (sDate, eDate) { // 获得时间段的每一天日期和周几
      this.dataList = []
      let weekDays = ['周天', '周一', '周二', '周三', '周四', '周五', '周六']
      for (let i = sDate; i <= eDate; i += (24 * 3600 * 1000)) {
        let hour = parseTime(i, '{y}-{m}-{d}').split('-')
        let newdate = new Date(hour[0], parseInt(hour[1] - 1), hour[2])
        this.dataList.push({
          dateTime: parseTime(i, '{y}-{m}-{d}'),
          weekDay: weekDays[newdate.getDay()],
          weekCode: newdate.getDay() === 0 ? 7 : newdate.getDay(),
          isShow: false
        })
      }
    },
    gethourData () { // 生成最终课时数据
      this.hourList = []
      this.deleteHour = 0
      if (this.isEditone) { // 编辑时初始数据
        console.log('初始数据')
        let hourDafault = this.dedaultData.listClassSchedule
        let weekDays = ['', '周一', '周二', '周三', '周四', '周五', '周六', '周天']
        for (let item of hourDafault) {
          this.hourList.push({
            dateTime: parseTime(item.beginDate, '{y}-{m}-{d}'),
            weekDay: weekDays[item.weekCode],
            weekCode: item.weekCode,
            monent: parseTime(item.startTime, '{h}:{i}') + '-' + parseTime(item.endTime, '{h}:{i}'),
            beginDate: item.beginDate,
            startTime: item.createTime,
            endTime: item.modifyTime,
            subjectName: item.subjectName,
            addressId: item.addressId,
            teacherId: item.teacherId,
            status: item.status
          })
        }
        this.deleteHour = this.hourList.length - this.dedaultData.lessonCount
        if (this.hourList.length > 0) {
          this.isEditone = false
        }
      } else {
        // console.log('computed')
        for (let i = 0; i < this.form.classSeting.length; i++) {
          let weekCode = Number(this.form.classSeting[i].weekCode)
          for (let item of this.form.classSeting[i].setArray) {
            let isRequired = []
            if (this.form.className.properties === 0) {
              delete item.className
              delete item.classCoach
            }
            for (let key in item) {
              if (item[key]) {
                isRequired.push(true)
              } else {
                isRequired.push(false)
              }
            }
            if (isRequired.includes(false)) {
              this.isClassSet = false
            } else {
              this.isClassSet = true
            }
            for (let datel of this.dataList) {
              if (datel.weekCode === weekCode && item['classTime'] !== '' && item['isError'] === 2) {
                if (!isRequired.includes(false)) {
                  this.hourList.push({
                    dateTime: datel.dateTime,
                    weekDay: datel.weekDay,
                    weekCode: datel.weekCode,
                    monent: parseTime(item['classTime'][0], '{h}:{i}') + '-' + parseTime(item['classTime'][1], '{h}:{i}'),
                    beginDate: new Date(datel.dateTime).getTime(),
                    startTime: new Date(item['classTime'][0]).getTime(),
                    endTime: new Date(item['classTime'][1]).getTime(),
                    subjectName: item.className,
                    addressId: item.classAdd ? item.classAdd.id : '',
                    teacherId: item.classCoach ? item.classCoach.id : '',
                    status: 0
                  })
                }
              }
            }
          }
        }
      }
    },
    handleDelteHour (index) {  // 删除课时
      this.hourList[index].status = 1
      this.setDeleteLength()
    },
    handleCancleHour (index) {  // 取消删除课时
      this.hourList[index].status = 0
      this.setDeleteLength()
    },
    setDeleteLength () { // 计算删除课时
      let hourLength = 0
      for (let item of this.hourList) {
        if (item.status === 1) {
          hourLength++
        }
      }
      this.deleteHour = hourLength
    }
  }
}
</script>
<style lang="scss">
.add_class{
  margin-top:30px;
  .el-input.is-disabled .el-input__inner,.el-input.is-disabled .el-input__inner::placeholder{
    color:#000;
  }
  .set_all{
    margin-bottom:0;
    text-align:center;
    color:#486a67;
  }
  .el-select{
    vertical-align: top;
  }
  .el-button.is-disabled, .el-button.is-disabled:hover{
    .el-icon-plus{
      color:#e5e5e5;
    }
  }
  .el-button--text{
    color:#2a82ee;
  }
  .el-button.is-disabled, .el-button.is-disabled:hover{
    color:#d0cfcf;
  }
  .time_list{
    margin-left:100px;
    font-size:12px;
    background:#fafafa;
    padding:25px;
    .el-col{
      line-height:16px;
      margin-bottom:20px;
    }
    .el-button{
      font-size:12px;
      line-height:16px;
      padding:0;
      vertical-align: top;
    }
    .color_grea{
      color:#ccc;
    }
    .class_total{
      .total_red{
        color:#f30;
      }
      .total_green{
        color:#00b28a;
      }
    }
  }
}
</style>
